package com.lx.servlet;

import com.alibaba.fastjson.JSON;
import com.lx.bean.FileTest;
import com.lx.dao.FileTestImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

//获取文件元数据接口：
//响应客户端 get 请求，客户端参数为接口 1 中返回的 UUID，返回值为 Json
//格式的元数据信息
@WebServlet("/SelectServlet")
public class SelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取到传过来的参数uuid
        String uuid = request.getParameter("uuid");
        System.out.println(uuid);
        //设置响应的字符编码
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        //创建FileTestImpl对象调用查询方法
        FileTestImpl fileTest = new FileTestImpl();
        List<FileTest> fileTests = fileTest.selectAllFile();
        //使用import com.alibaba.fastjson.JSON;把数据转换为json格式
        String fileJson = JSON.toJSONString(fileTests);
        //数据显示到前端界面上
        OutputStream out = response.getOutputStream();
        out.write(fileJson.getBytes("UTF-8"));
        out.flush();


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
